// pages/userinfoDetail.js
const baseUrl = require('../../config').baseUrl
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        userInfo: '',
        username: '', // 名字
        deptname: '', // 单位名称
        email: '', // 邮箱
        tel: '', // 手机号
        deptid: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            userInfo: wx.getStorageSync('userInfo'),
            username: app.globalData.username,
            deptname: app.globalData.deptname,
            email: app.globalData.email,
            tel: app.globalData.tel,
            deptid: app.globalData.deptid
        })
    },
    changeTel (e) {
        this.setData({
            tel: e.detail.value
        })
    },
    changeEmail (e) {
        this.setData({
            email: e.detail.value
        })
    },
    changeUsername (e) {
        this.setData({
            username: e.detail.value
        })
    },
    changeDeptname (e) {
        this.setData({
            deptname: e.detail.value
        })
    },
    saveUserInfo () {
        let tel = this.data.tel
        let email = this.data.email
        if (!(/^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$/.test(email)) && email !== '') {
            wx.showToast({
                title: '请输入正确的邮箱',
                icon: 'none',
                duration: 1000
            })
            return false
        } else if (!(/^1[3456789]\d{9}$/.test(tel)) && tel !== '') {
            wx.showToast({
                title: '请输入正确的手机号',
                icon: 'none',
                duration: 1000
            })
            return false
        }
        let This = this
        wx.request({
            url: baseUrl,
            header: {
                "content-type": "application/x-www-form-urlencoded"
            },
            data: {
                data: JSON.stringify({
                    business: "SaveUserInfo",
                    data: {
                        account: app.globalData.account,
                        username: this.data.username, // 名字
                        deptname: this.data.deptname, // 单位名称
                        email: this.data.email, // 邮箱
                        deptid: this.data.deptid, // 用户单位id
                        tel: tel // 手机号
                    }
                })
            },
            method: 'POST',
            success (res) {
                if (res.data.code === 0) {
                    wx.setStorageSync('isReopen', true)
                    wx.setStorageSync('modifiUserInfo', true)
                    app.globalData.deptname = This.data.deptname
                    app.globalData.username = This.data.username
                    app.globalData.email = This.data.email
                    app.globalData.tel = This.data.tel
                    if (res.data.deptid) {
                        app.globalData.deptid = res.data.deptid
                    }
                    wx.navigateBack({
                        delta: 1
                    })
                } else {
                    wx.showToast({
                        title: res.data.info,
                        icon: 'none',
                        duration: 1000
                    })
                }
            },
            fail: (error) => {
                wx.showToast({
                    title: error.data.info,
                    icon: 'none',
                    duration: 1000
                })
            }
        })
    }
})